package indi.zjh.dp.startup;

import java.io.Serializable;

public class ServerStatus implements Serializable{
	private static ServerStatus instance = null;
	
	// 服务器角色定义, 主节点, 从节点
	public static final int SLAVE = 0;
	public static final int MASTER = 1;
	// 心跳更新时间
	protected long updateTime = 0;
	// 处理请求个数
	protected long requestCount = 0;
	// 业务版本号
	protected long version = 0;
	// 角色
	protected int role = 0;
	
	private ServerStatus()
	{
		this.updateTime = (int)(System.currentTimeMillis()/1000);
	}
	
	public static ServerStatus getInstance()
	{
		if(instance == null)
		{
			instance = new ServerStatus();
		}
		return instance;
	}
	
	
	public void becomeSlave()
	{
		this.role = SLAVE;
	}
	
	public void becomeMaster()
	{
		this.role = MASTER;
	}
	
	public boolean isSlave()
	{
		return this.role == SLAVE;
	}
	
	public boolean isMaster()
	{
		return this.role == MASTER;
	}
	
	public void heartbeat()
	{
		this.updateTime = (int)(System.currentTimeMillis()/1000);
	}
	
	public void doRequest()
	{
		++this.requestCount;
	}
	
	public void clearRequest()
	{
		this.requestCount = 0;
	}
	
	public long getUpdateTime()
	{
		return this.updateTime;
	}
}
